package com.example.fantacylib

import android.annotation.SuppressLint
import android.bluetooth.BluetoothClass.Device
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.databinding.DataBindingUtil
import com.camera.BuildConfig
import com.example.baseutils.AppUtils
import com.example.baseutils.Constance
import com.example.baseutils.MySharedPreferences
import com.example.baseutils.NetUtil
import com.example.baseutils.activity.BaseLoadActivity
import com.example.baseutils.read
import com.example.baseutils.showMyToast
import com.example.baseutils.showToast
import com.example.baseutils.start
import com.example.baseutils.ui.ProgressDialog
import com.example.fantacylib.databinding.ActivityDeviceBinding
import com.example.net.http.FantacyRetrofitCreator
import com.example.printlib.PrintMainActivity
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch

/**
 * 设备详情页
 * author：lp
 * 2024年09月05日16:14:42
 * */
class DeviceActivity : BaseLoadActivity() {
    private  val TAG = "DeviceActivity"
    private lateinit var binding:ActivityDeviceBinding
    private val mainScope = CoroutineScope(Dispatchers.Main)
    override fun onLoad() {
        pullDeviceInfo()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding=DataBindingUtil.setContentView(this,R.layout.activity_device)
        binding.btnBack.setOnClickListener { finish() }
        binding.btnPull.setOnClickListener { pullDeviceInfo()  }
        binding.btnPrintTest.setOnClickListener {  PrintMainActivity().start(this@DeviceActivity)}
        binding.tvPhone.text= MySharedPreferences.read(Constance.SERVICE_CALL,"000")
    }

    @SuppressLint("SetTextI18n")
    private fun pullDeviceInfo(){
        binding.tvIp.text=NetUtil.getHostAddress()?:"未获取到"
        mainScope.launch {
            val progress=ProgressDialog(this@DeviceActivity).init(false)
            progress.showProgress("拉取数据中")
            val result= FantacyRetrofitCreator.getDeviceInfo()
            if(result!=null){
                Log.i(TAG, "onLoad: $result")
                binding.tvSoftVer.text=result.softVersionNo
                binding.tvDevices.text=result.no?:AppUtils.getLocalMacAddressFromWifiInfo()
                binding.tvScene.text=result.spuName?:"未获取到"
                binding.tvCount.text=result.countdown?.toString()+"秒"
                "拉取成功".showToast(this@DeviceActivity)
            }
            else
                "拉取失败".showToast(this@DeviceActivity)
            progress.dismissDialog()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        mainScope.cancel()
    }
}